package com.bytech.admin.biz.service.systemSetting.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bytech.admin.biz.enums.SystemSettingEnum;
import com.bytech.admin.biz.mapper.systemSetting.SystemSettingMapper;
import com.bytech.admin.biz.service.systemSetting.SystemSettingService;
import com.bytech.common.biz.entity.system.SystemSetting;
import com.bytech.common.biz.service.base.BasicServiceImpl;
import com.bytech.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Zhao
 * @Create 2021-08-14
 */
@Service
public class SystemSettingServiceImpl extends BasicServiceImpl<SystemSettingMapper, SystemSetting> implements SystemSettingService {

    @Resource
    private SystemSettingMapper systemSettingMapper;

    @Override
    public List<SystemSetting> getAll(String settingId) {
        if (StringUtils.isBlank(settingId)) {
            return null;
        }
        LambdaQueryWrapper<SystemSetting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemSetting::getSettingId, settingId).orderByDesc(SystemSetting::getVersion);
        return systemSettingMapper.selectList(queryWrapper);
    }

    @Override
    public SystemSetting getLatest(String settingId) {
        if (StringUtils.isBlank(settingId)) {
            return null;
        }
        List<SystemSetting> settings = getAll(settingId);
        if (!CollectionUtils.isEmpty(settings)) {
            return settings.get(0);
        }
        return null;
    }

    @Override
    public SystemSetting getBySettingIdAndVersion(String settingId, Integer version) {
        if (StringUtils.isBlank(settingId) || version == null) {
            return null;
        }
        LambdaQueryWrapper<SystemSetting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemSetting::getSettingId, settingId).eq(SystemSetting::getVersion, version);
        return systemSettingMapper.selectOne(queryWrapper);
    }
}
